<?php

use HeadlessChromium\BrowserFactory;
use Swoole\Coroutine\Http\Client;
use Swoole\Timer;
function gao()
{

    $browserFactory = new BrowserFactory();

    $browserFactory->setOptions([
        'windowSize' => [450, 844],
        'userAgent'  => 'Mozilla/5.0 (Linux; Android 12; Mi 10 Build/SKQ1.211006.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/116.0.0.0 Mobile Safari/537.36 XWEB/1160083 MMWEBSDK/20231202 MMWEBID/8153 MicroMessenger/8.0.46.2540(0x28002E37) WeChat/arm64 Weixin NetType/5G Language/zh_CN ABI/arm64',
    ]);
// starts headless Chrome
    $browser = $browserFactory->createBrowser();

    try {
        // creates a new page and navigate to an URL
        $page = $browser->createPage();
        //http://121.62.23.186:7700/agent
        $page->navigate('http://127.0.0.1:9977/b.html')->waitForNavigation();
        sleep(2);
        // get page title
        // $pageTitle = $page->evaluate('document.title')->getReturnValue();

        // screenshot - Say "Cheese"! 😄
        $page->screenshot()->saveToFile(BASE_PATH . 'bar4.png');

        // pdf
        // $page->pdf(['printBackground' => false])->saveToFile('/foo/bar.pdf');
    } finally {
        // bye
        $browser->close();
    }

    return;

    // $mail = new PHPMailer(true);

    // try {
    //     //Server settings
    //     // $mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
    //     $mail->isSMTP(); //Send using SMTP
    //     $mail->Host       = 'smtp.qq.com'; //Set the SMTP server to send through
    //     $mail->SMTPAuth   = true; //Enable SMTP authentication
    //     $mail->Username   = '77871399@qq.com'; //SMTP username
    //     $mail->Password   = 'aykntytvgeyjbjjc'; //SMTP password
    //     $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
    //     $mail->Port       = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    //     //Recipients
    //     $mail->setFrom('77871399@qq.com', 'Mailer');
    //     $mail->addAddress('m@lu8.fun', 'Joe User'); //Add a recipient

    //     //Content
    //     $mail->isHTML(true); //Set email format to HTML
    //     $mail->Subject = 'Here is the subject';
    //     $mail->Body    = 'This is the HTML message body <b>in bold!</b>';

    //     $mail->send();

    // } catch (Exception $e) {
    //     echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    // }

    // return;

    // h3yrzj5.ipqmhup.museum.mw:9000/clientWs?ucode=aj82c9JU&fingerprint=xhg7ihNhmZ
    $client = new Client('h3yrzj5.ipqmhup.museum.mw', 9000);

    // $opt = [
    //     'http_proxy_host' => '114.99.156.134',
    //     'http_proxy_port' => 44123,
    //     // 'http_proxy_user'     => '1064444458135080960',
    //     // 'http_proxy_password' => '0g7xvRal',
    // ];
    // $client->set($opt);

    // $client->get('/clientWs?ucode=aj82c9JU&fingerprint=xhg7ihNhmZ');
    loger($client);
    // echo $client->body;
    // $client->close();
    $ret = $client->upgrade('/clientWs?ucode=aj82c9JU&fingerprint=xhg7ihNhmZ');
    loger($ret);
    if ($ret) {
        while (true) {
            $client->push('{"type":"ping"}');
            var_dump($client->recv());
            \Co::sleep(4);
        }
    }

}

function te($input)
{
    $maxDay = count($input);

    for ($i = 0; $i < $maxDay; $i++) {
        // $result[$i] = 0;
        // for ($n = $i + 1; $n < $maxDay; $n++) {
        //     if ($input[$n] > $input[$i]) {
        //         $result[$i] = $n - $i;
        //         break;
        //     }
        // }
        // continue;
        //当第二天的价格比第一天高的时候，记录第一天的结果为1
        //这个条件不遍历后续价格，减少循环次数，提高性能
        if ($input[$i] < $input[$i + 1]) {
            $result[$i] = 1;
            continue;
        }
        // loger(($i) . ' ' . $input[$i]);

        for ($n = $i + 1; $n < $maxDay; $n++) {
            if ($input[$i] < $input[$n]) {
                $result[$i] = $n - $i;
            }
        }
    }
    $result[$maxDay - 1] = 0; //结果的最后一天值始终会为0

    loger($result);
}

function strRand($length)
{
    $randomString = '';
    // 生成指定长度的随机字节
    $bytes = random_bytes($length);
    // 将随机字节转换为十六进制字符串
    for ($i = 0; $i < $length; $i++) {
        $randomString .= bin2hex($bytes[$i]);
    }
    return $randomString;
}

function aa($value = '')
{
    // $a=new RedisConnecter();

    // loger($a->run()->handler->info());
    // Rs::info();
    // loger(Rs::set('a',time()));
    // loger(Rs::indexCreate());
    // loger(Db::table('user')->where('id',1)->select(true));

    $index = [
        'id'      => ['NUMERIC', 'SORTABLE'],
        'status'  => ['NUMERIC', 'SORTABLE'],
        'gmCount' => ['NUMERIC'],
        'lqCount' => ['NUMERIC'],
        'tjCount' => ['NUMERIC'],
        'created' => ['NUMERIC'],
        // 'key' => ['TEXT'],
        'worker'  => ['TAG'],
    ];
    // $a = app('redis','default')->index('task','star:task',$index);

    // Rs::ftCreate('create', 'tasker', 'star:task', $index);
    // loger(Rs::ft('tasker')->delete());
    // loger(Rs::ft('tasker')->create('task', $index));
    // loger(Rs::ft('tasker')->info());
    // loger(Rs::ft('tasker')->delete());
    // loger(Rs::json('task')->arrAppend(13, 'worker', 'sss'));
    // $s =
    //     loger($s);
    // loger(Rs::json('tasker')->insert(['name' => 4, 'worker' => []]));
    // loger(Rs::json('tasker')->info(6));
    // loger(Rs::json('tasker')->arrAppend(9, 'worker', '0.5s'));
    loger(Rs::json('task')->delete(10, 'key2'));
    // loger(Rs::json('task')->id(10)->incr('key2.c'));
    // loger(Rs::json('task')->id(10)->update('key2', ['c' => 3]));
    // for ($i = 0; $i < 10000; $i++) {
    //     $ins = ['id' => $i, 'status' => 1, 'key' => strRand(35), 'gmCount' => mt_rand(10, 1000), 'lqCount' => 0, 'pushCount' => 0, 'created' => time(), 'worker' => []];
    //     Rs::json('task')->insert($ins);
    // }
    // tu(function () {
    //     for ($i = 1; $i <= 10000; $i++) {
    //         $a = Rs::ft('tasker')->where("@id:[$i $i]")->search();
    //         // loger($a);
    //     }
    // });

    return;
    // $v=config('db.default');

    // $pool=Pools::init(fn()=>new Connecter($v));
    // for ($i=0; $i < 1000; $i++) {
    //     $p = $pool->pull();
    //     $p->handler->query('SHOW TABLES')->fetch();
    //     $pool->push($p);
    //     //  for ($i=0; $i < 50; $i++) {
    //     //        go(function()use($pool){
    //     //      $p = $pool->pull();
    //     //      $p->handler->query('SHOW TABLES')->fetch();
    //     //      $pool->push($p);
    //     // });
    //     //  }
    //     // for ($i=0; $i < 5; $i++) {
    //     //     go(function()use($pool){
    //     //      $p = $pool->pull();
    //     //      $p->handler->query('SHOW TABLES')->fetch();
    //     //      $pool->push($p);
    //     // });
    //     // }
    //     // \Co::sleep(0.001);
    // }
    // loger($pool->num);
    // go(function()use($pool){
    //  $p = $pool->pull();
    //  $p->handler->query('SHOW TABLES')->fetch();
    //  $pool->push($p);
    // });
    // go(function()use($pool){
    //  $p = $pool->pull();
    //  $p->handler->query('SHOW TABLES')->fetch();
    //  $pool->push($p);
    // });
    // sleep(1);
    // loger($pool->pool->length());
    // return;
    timer::tick(1000 * 5, function () use ($pool) {
        try {
            go(function () use ($pool) {
                $p = $pool->pull();
                $p->handler->query('SHOW TABLES')->fetch();
                loger($p);
                $pool->push($p);
            });
            // go(function()use($pool){
            //  $p = $pool->pull();
            //  $p->handler->query('SHOW TABLES')->fetch();
            //  $pool->push($p);
            // });
        } catch (\PDOException $e) {
            loger($e);
        }

    });

}
